//
//  KDSBodyView.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/25.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDSBodyView.h"
#import "KDSearchView.h"
//#import "KDRGBModel.h"
#import "KDSearchHistoryTool.h"
#import "SVProgressHUD.h"
#import "KDSearchHistory.h"
#import "KDSBaseBoard.h"
#import "KDWSBaseBoard.h"
#import "KDMBaseBoardModel.h"
#import "KDGetSaerchListParam.h"
#import "KDSearchHistoryCell.h"
#define BTN_WIDTH screenW * 0.18
#define FILTERBTN_WIDTH 36//筛选按钮的宽度
@interface KDSBodyView ()<UITableViewDelegate, UITableViewDataSource, KDSearchViewDelegate, KDSearchHistoryCellDelegate>
/***  头部scrollView*/
@property (nonatomic, weak) UIScrollView *detailScrollView;

/***  头部按钮个数*/
@property (nonatomic, strong) NSArray *headNameArr;
/***  历史纪录数据*/
@property (nonatomic, strong) NSArray *infoArr;
/**
 *  头部scrollView旁边的筛选按钮
 */
@property (nonatomic, weak) UIButton *filterBtn;
/***  被选中的头部按钮*/
@property (nonatomic, strong) UIButton *selectDetailBtn;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) KDSearchView *searchView;
/***  当前的的搜索模型*/
@property (nonatomic, strong) KDSearchHistory *currentSearchModel;
/** 当前的baseBoardModel*/
@property (nonatomic, strong) KDMBaseBoardModel *baseBoardModel;
/** 默认的baseBoardModel*/
@property (nonatomic, strong) KDMBaseBoardModel * defaultBaseBoardModel;
/** 右边弹出的选择界面*/
@property (nonatomic, weak) KDMBaseBoard *baseView;
/** 右边弹出的选择界面的遮盖*/
@property (nonatomic, weak) UIButton *cover;
/**
 *  下一个界面需要的请求参数
 */
@property (nonatomic, strong) KDGetSaerchListParam *param;

@property (nonatomic, strong) NSDateFormatter *fmt;
@end
@implementation KDSBodyView


#pragma mark - 懒加载区
- (NSDateFormatter *)fmt
{
    if (!_fmt) {
        _fmt = [[NSDateFormatter alloc]init];
        _fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];;
        _fmt.dateFormat = @"yyyy-MM-dd";
    }
    return _fmt;
}
- (KDMBaseBoardModel *)baseBoardModel
{
    if (!_baseBoardModel) {//那里找不到了，就自己建
        _baseBoardModel = [[KDMBaseBoardModel alloc]init];
        _baseBoardModel.webSrcType = @"0";
//        NSDate *newdate = [[NSDate date] initWithTimeInterval:- (24 *60 * 60 * 6 )sinceDate:[NSDate date]];
        _baseBoardModel.startTime = @"1970-01-01";//[self.fmt stringFromDate:newdate];
        _baseBoardModel.endTime = @"";
        _baseBoardModel.timeIndex = @"9";
        _baseBoardModel.docType = @"0";
        _baseBoardModel.bussiness = @"0";
        _baseBoardModel.province = 0;
        _baseBoardModel.city = 0;
        self.defaultBaseBoardModel = _baseBoardModel ;
    }
    return _baseBoardModel;
}
- (KDGetSaerchListParam *)param{
    if (!_param) {//懒加载默认值
        _param = [[KDGetSaerchListParam alloc]init];
        [self paramGetData];
    }
    return _param;
}
/**
 *  param更新数据
 */
- (void)paramGetData{
    _param = [[KDGetSaerchListParam alloc]init];
    _param.startTime = self.baseBoardModel.startTime;
    _param.endTime = self.baseBoardModel.endTime;
    _param.property = self.baseBoardModel.docType;
    if (self.type) {
        _param.weiboClued = self.baseBoardModel.webSrcType;
        _param.kindType = nil;
    }else{
        _param.kindType = self.baseBoardModel.webSrcType;
    }
    
    _param.province = self.baseBoardModel.province;
    _param.city = self.baseBoardModel.city;
}
- (KDSearchHistory *)currentSearchModel{
    if (!_currentSearchModel) {
        _currentSearchModel = [[KDSearchHistory alloc]init];
        _currentSearchModel.type = self.type;
    }
    return _currentSearchModel;
}
- (NSArray *)headNameArr{
    if (!_headNameArr) {
        if (self.type) {
            _headNameArr  = @[@" 全部",@"含图片", @"含视频", @"含音乐", @"含短链"];
        }else{
            _headNameArr  = @[@"全部",@"论坛", @"博客", @"视频", @"微博",  @"微信", @"新闻", @"平媒",@"境外",@"维语"];
        }
    }
    return _headNameArr;
}
- (void)setType:(NSInteger)type{
    _type = type;
//    [self setUpHeadScrollView];
//    [self setUpFilterBtnBtn];
    [self setUpSearchView];
    [self setUpTablView];
}
- (NSArray *)infoArr{
    if (!_infoArr) {
                _infoArr = [NSArray array];
        _infoArr = [KDSearchHistoryTool searchHistoryListWithType:self.type];
    }
    return _infoArr;
}
- (void)setBack:(BOOL)back{
    _back = back;
    if (back) {
        _infoArr = [KDSearchHistoryTool searchHistoryListWithType:self.type];
        [self.tableView reloadData];
    }
}
#pragma mark - 板块布置区
/**
 *  初始化方法
 */
+ (instancetype)bodyViewWithFrame:(CGRect)frame
{
    return [[self alloc]initWithFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = BackGroundColor;
    }
    return self;
}
/**
 *  初始化searchView
 */
- (void)setUpSearchView{
    KDSearchView *searchView = [[KDSearchView alloc]init];
    searchView.frame = CGRectMake(10, CGRectGetMaxY(self.detailScrollView.frame) +7, screenW - 20, 35);
    searchView.placeholder = @"请输入关键词";
    searchView.returnSearch = YES;
    searchView.SearchViewBackGroundColor = WQColor(255, 255, 255);
    searchView.radius = 0.02;
    searchView.delegate = self;
    searchView.textColor = [UIColor darkGrayColor];
    searchView.layer.cornerRadius = 8;
    searchView.layer.borderWidth = 1;
    searchView.layer.borderColor = WQColor(238, 241, 243).CGColor;
    self.searchView = searchView;
    [self addSubview:searchView];
}
- (void)setUpTablView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0  , CGRectGetMaxY(self.searchView.frame) +7, screenW,  self.height - CGRectGetMaxY(self.searchView.frame) ) style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    //    tableView.tag = 1000;
    tableView.tableFooterView = [UIView new];
    tableView.separatorColor = BackGroundColor;
    tableView.backgroundColor = BackGroundColor;
    [self addSubview:tableView];
    
}
- (void)setSelectDetailBtn:(UIButton *)selectDetailBtn
{
    _selectDetailBtn.selected = NO;
    _selectDetailBtn = selectDetailBtn;

    _selectDetailBtn.selected = YES;
}

#pragma mark - 事件点击区
/***  细节Btn被点击 */
-(void)detailBtnDidClick:(UIButton *)btn
{
    self.selectDetailBtn = btn;
}


#pragma mark - 网络请求区

#pragma mark - 通知接收区

#pragma mark - KDSearchViewDelegate
/**
 *  搜索按钮被点击了
 */
- (void)KDSearchViewWithSearchBtnClick:(KDSearchView *)searchView{
    self.currentSearchModel.searchText = searchView.text;
    self.currentSearchModel.type = self.type;
    if (self.currentSearchModel.searchText.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入搜索内容！" maskType:SVProgressHUDMaskTypeGradient];
        return;
    }
    if (self.param) {
    }
    [self paramGetData];
    [KDSearchHistoryTool saveSearchHistory:self.currentSearchModel];
    self.param.keyword = self.currentSearchModel.searchText;
    self.param.type = self.type;
    if ( self.selectDetailBtn ) {
        if (self.type) {//如果是微博搜索就传含什么鬼
            self.param.weiboClued = [NSString stringWithFormat:@"%ld",self.selectDetailBtn.tag - 100] ;
        }else{
            self.param.kindType = [NSString stringWithFormat:@"%ld",self.selectDetailBtn.tag - 100] ;
        }
    }
    if (_delegate && [_delegate respondsToSelector:@selector(KDSBodyView:SearchBtnClickWithSearchModel:)]) {
        [_delegate KDSBodyView:self SearchBtnClickWithSearchModel:self.param];
    }
}
#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.infoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KDSearchHistoryCell *cell = [KDSearchHistoryCell cellWithTableView:tableView];
    cell.delegate = self;
    cell.model = self.infoArr[indexPath.row];
//    cell.model.row = indexPath.row;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KDSearchHistory *history = self.infoArr[indexPath.row];
    if (self.param) {
    }
    self.param.keyword = history.searchText;
    self.param.type = history.type;
    if (_delegate && [_delegate respondsToSelector:@selector(KDSBodyView:WithModel:didSelectRowAtIndexPath:)]) {
        [_delegate KDSBodyView:self WithModel: self.param didSelectRowAtIndexPath:indexPath];
    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UILabel *noticeLb = [[UILabel alloc]init];
    noticeLb.frame = CGRectMake(10, 10, screenW, 30);
    noticeLb.text = @"  最近搜索";
    noticeLb.font = [UIFont systemFontOfSize:13];
    noticeLb.textColor = [UIColor lightGrayColor];
    noticeLb.backgroundColor = [UIColor clearColor];
    noticeLb.textAlignment = NSTextAlignmentLeft;
    return noticeLb;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30;
}

#pragma mark - KDSearchHistoryCellDelegate
/**
 *  删除按钮被点击了
 */
- (void)KDSearchHistoryCell:(KDSearchHistoryCell *)cell WithHistory:(KDSearchHistory *)history{
    [KDSearchHistoryTool deleteHistory:history];
    self.infoArr = [KDSearchHistoryTool searchHistoryListWithType:history.type];
    [self.tableView reloadData];
}

@end
